#!/usr/bin/env perl -w
# Time-stamp: <05 Dec 01 15:20:50 giron1>

=head1 NAME

dicgen.perl - generate dictionaries

=head1 SYNOPSIS

dicgen.perl [commands] [options] I<vocabulary> I<dictionary>

see also:

L<"COMMANDS">,
L<"OPTIONS">

at the end of this document.

=head1 DESCRIPTION

dicgen.perl generates a I<dictionary> using the original I<vocabulary>
file. The corresponding phonemic transcription is computed by using an
automatic grapheme to phoneme conversion.

=head1 SEE ALSO

The following packages description:

L<STC_DEF|STC_DEF_HTML/STC_DEF>,
L<OPTIONS|OPTIONS_HTML/OPTIONS>,
L<SIDL_VOCAB|SIDL_VOCAB_HTML/SIDL_VOCAB>

=head1 AUTHOR

 $Id: dicgen.perl,v 1.1 2007/10/08 09:26:02 schweitz Exp $
 Franck Giron

=head1 LOG

 $Log: dicgen.perl,v $
 Revision 1.1  2007/10/08 09:26:02  schweitz
 version 2.4, lib vergessen

 Revision /main/8  22-Jan-2003.09:54:15  giron1
 Changing header to /usr/bin/env perl.
 
 Revision /main/7  5-Dec-2001.17:57:33  giron1
 Changing the call to expandphono to correspond to the new
 definition. Made the test with the external dictionary correctly.
 
 Revision /main/6  15-Oct-2001.15:09:25  giron1
 Adding external dictionary reading option.
 
 Revision 1.4  2001/03/05 16:58:09  giron1
 aibo dictionary added

 Revision 1.3  2000/06/28 14:49:49  giron1
 added the option iso2tex for compatibility with german Aligner.

 Revision 1.2  2000/04/26 08:30:01  giron1
 Pod dosumentation updated


=cut

#----------------------------------------------------------------------
# perl packages
#----------------------------------------------------------------------

use strict;
use Misc;
use SIDL_VOCAB;
use OPTIONS;
use STC_DEF;

#----------------------------------------------------------------------
# MAIN
#----------------------------------------------------------------------
my $STC_Vocab_files = givehash STC_DEF;

# Define the set of options

my $OptList = new OPTIONS;

$OptList->debug('Do' => 0, 'Usage' => 'debugging mode');
$OptList->uscore('Do' => 0, 'Usage' => 'put underscore instead of spaces between words');
$OptList->iso2tex('Do' => 0, 'Usage' => 'converts original input from iso to tex for old Aligner');
$OptList->isoout('Do' => 0, 'Usage' => 'let original input to iso');
$OptList->lang('string' => 'deu', 'Usage' => "<Iso3>\t: language of dictionary");
$OptList->format('string' => 'vrecog', 'Usage' => "<str>\t: dictionary format");
$OptList->dic('string' => '', 'Usage' =>"<str>\t: external dictionary used");

# Command processing

$OptList->GetOpt || die "Check your options\n";

my $numargs = @ARGV;
my $usage = !$numargs || ("$ARGV[0]" eq '-h');

if ($usage) {
  $OptList->Usage('vocabulary dictionary');
}

my $format = $OptList->{'format'}{'string'};
my $lang = $OptList->{'lang'}{'string'};
my $underscore = $OptList->{'uscore'}{'string'};


# Extract path and keyword information if any from the rest of
# the argument line

my $path = '';
my $keyw = $ARGV[0];

if ($ARGV[0] =~ m/([^\/]+)$/) {
  $path = $`;
  $keyw = $1;
}



$OptList->{'debug'}{'Do'} && warn "format=$format, keyw=$keyw, path=$path\n";

# Determines vocabulary file from keyword
my $voc_file = $path . $keyw;

if (exists($STC_Vocab_files->{$keyw}) ) {
  $voc_file = $path . $STC_Vocab_files->{$keyw};
}

$OptList->{'debug'}{'Do'} && warn "vocfile=$voc_file\n";

# Read the words in a hash

my $Vocab = new SIDL_VOCAB;
#my $cons = 'BCDFGNRSZbdfgklmnpstvwz';
#my $vow = 'EHJO269aeijouy';

if ( -r $voc_file ) {
  open VOCFILE, "<$voc_file" || die "cannot open $voc_file";
} else {die "cannot read $voc_file";}

while (<VOCFILE>) {
    chop;
    # replaces all punctuation chars with space
    if ($OptList->{'format'}{'string'} ne 'aibo') {s/[_\-?,;.!:]/ /g;}
    # suppresses final spaces
    s/\s+$//g;
    # replaces many spaces through a single one
    s/\s{2,}/ /g;
    # converts eventually iso to esc for compatibility
    # with old aligner
    if ($OptList->{'iso2tex'}{'Do'}) { $_ = iso_to_tex($_);}
    if ($OptList->{'uscore'}{'Do'}) {
      # replaces spaces with underscore
      s/\s/_/g;
      $Vocab->{$lang}{$_}[0] = '';
    } 
    else {
      # split line in single words
      my @list = split /\s+/;
      foreach (@list) {$Vocab->{$lang}{$_}[0] = '';}
    }
}
close VOCFILE;


# External dictionary defined

my $Extdic = new SIDL_VOCAB;

if ($OptList->{'dic'}{'string'} ne '') {
  $Extdic->readdic($OptList->{'dic'}{'string'},$OptList->{'format'}{'string'});
  my $graph;
  my $head = '';
  my $tail = '';
  my $debug = 1;
  foreach $lang (sort keys %{$Vocab}) {
    $debug and warn "Treating $lang:\n";
    foreach $graph (sort keys %{$Vocab->{$lang}}) {
      warn "graph: $graph\n";
      my @wordlist = split '_',$graph;
      my $string = '';
      foreach (@wordlist) {
	if (exists($Extdic->{$_})) {
	  warn $_, join(',',@{$Extdic->{$_}}), "\n";
	  $string .= ' (' . join('|',@{$Extdic->{$_}}) . ') (_sil_|)';
	}
      }
      $string =~ s/\(\S+\)$//;
      warn "string: $string\n";
      # create an expansion for each possible pause
      my $phonowpause = new GRAPH2PHONE;
      $phonowpause->expandphono($head,$string,$tail);
      warn join(',',@$phonowpause), "\n";
      my $final;
      foreach $final (@$phonowpause) {
	$final =~ s/ +/ /g;
	print "$graph\t[$graph]\t$final\n";
      }
    }
  }
} else {
  if (not $OptList->{'isoout'}{'Do'}) {SIDL_VOCAB::setuseescencode(1);}
  $Vocab->fillpronunciations;
  $Vocab->writedic("$ARGV[1]",$format);
}

__END__

=head1 SYNOPSIS

dicgen.perl [commands] [options] vocabulary dictionary

see also:

L<"COMMANDS">,
L<"OPTIONS">
at the end of this document.

=head1 COMMANDS

The following commands are defined:

=over 10

=item -debug	

 debugging mode

=item -iso2tex	

 converts original input from iso to tex for old Aligner

=item -isoout	

 let original input to iso

=item -uscore	

 put underscore instead of spaces between words

=back

=head1 OPTIONS

The following options are defined:

=over 14

=item -dic I<str>	

 external dictionary used 
 (default: )

=item -format I<str>	

 dictionary format 
 (default: vrecog)

=item -lang I<Iso3>	

 language of dictionary 
 (default: deu)

=back
